﻿'Nombre:                         Manejador
'Descripcion:                    Clase que controla todas las operaciones sobre el catalogo de bancos
'Dependencias:                 
'Fecha de creación:              20090613
'Autor:                          VMORALES
'Fecha de modifcación:         
'Responsable:                  
'Num. de modifcación:            0

Option Explicit On
Option Strict On

#Region "Imports"

Imports System.Collections.Specialized
Imports System.ComponentModel

#End Region

Namespace Banco

    ''' <summary>
    ''' Clase que controla todas las operaciones sobre el catalogo de bancos
    ''' </summary>
    Public Class Manejador

        Public Shared Event BancosActivosCambiados()

#Region "Constantes"

#End Region

#Region "Miembros"

        Private Shared _dao As IBancoDao = DirectCast(Application.Ctx.GetObject("BancoDao"), IBancoDao) 'Clase de acceso a los datos en la base de datos
        Public Shared Event ErrorPersistencia As ErrorPersistenciaHandler
        Public Delegate Sub ErrorPersistenciaHandler(ByVal dataProvider As Manejador, ByVal e As Exception)

#End Region

#Region "Propiedades"

        Public Shared Property Seleccion() As Entidades.BancoDTO
            Get
                Return Nothing
            End Get
            Set(ByVal value As Entidades.BancoDTO)
            End Set
        End Property

        Public Shared Property BancosActivos() As Entidades.BancoDTO()
            Private Set(ByVal value As Entidades.BancoDTO())
            End Set
            Get
                Return Nothing
            End Get
        End Property

#End Region

#Region "Contructores"

#End Region

#Region "Funciones"

        Public Function ObtenerBancos() As BancosUI
            Dim bancos As BancosUI = New BancosUI()

            AddHandler bancos.ItemEndEdit, AddressOf BancosItemEndEdit
            AddHandler bancos.CollectionChanged, AddressOf ColeccionBancosCambiada

            If _dao.ObtenerCantidad() > 0 Then
                For Each b As Entidades.BancoDTO In _dao.Obtener()
                    bancos.Add(New BancoUI(b))
                Next
            Else
                bancos.Add(New BancoUI())
            End If

            Return bancos

            ''http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx
        End Function

        Sub ColeccionBancosCambiada(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
            Dim banco As BancoUI

            Select Case e.Action
                Case NotifyCollectionChangedAction.Remove
                    For Each item As Object In e.OldItems
                        banco = TryCast(item, BancoUI)

                        If Not banco Is Nothing Then
                            Try
                                _dao.Eliminar(banco.DTO)
                            Catch ex As Exception
                                RaiseEvent ErrorPersistencia(Me, ex)
                            End Try
                        End If
                    Next
            End Select
        End Sub

        Sub BancosItemEndEdit(ByVal sender As IEditableObject)
            Dim banco As BancoUI = TryCast(sender, BancoUI)

            If Not banco Is Nothing Then
                Try
                    If _dao.Existe(banco.DTO) Then
                        _dao.Actualizar(banco.DTO)
                    Else
                        _dao.Agregar(banco.DTO)
                    End If
                Catch ex As Exception
                    RaiseEvent ErrorPersistencia(Me, ex)
                End Try
            End If
        End Sub

#End Region

    End Class

End Namespace